package jaxb.metagrammar;

import javax.xml.bind.annotation.adapters.XmlAdapter;

import jaxb.metagrammar.OperatorAdaptedLinkedHashMultimap.MultimapEntry;
import metagrammar.Operator;
import metagrammar.Transform;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.LinkedHashMultimap;

public class OperatorLinkedHashMultimapAdapter extends
        XmlAdapter<OperatorAdaptedLinkedHashMultimap, LinkedHashMultimap<Operator, Transform>> {
    
    @Override
    public LinkedHashMultimap<Operator, Transform> unmarshal(OperatorAdaptedLinkedHashMultimap v)
    throws Exception
    {
        ImmutableList<MultimapEntry> entries = v.getEntries();
        LinkedHashMultimap<Operator, Transform> multiMap = LinkedHashMultimap.create(entries.size(), 1);
        for(MultimapEntry entry : entries) {
            multiMap.putAll(entry.getKey(), entry.getValues());
        }
        return multiMap;
    }
    
    @Override
    public OperatorAdaptedLinkedHashMultimap marshal(LinkedHashMultimap<Operator, Transform> v)
    throws Exception
    {
        return new OperatorAdaptedLinkedHashMultimap(v);
    }
}
